package com.example.first.utils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

public class Utils {

    public static String getRealPath(Context context,Intent data){
        // 判断手机系统版本号
        if (Build.VERSION.SDK_INT >= 19) {
            // 4.4及以上系统使用这个方法处理图片
            if (!hasManageExternalStoragePermission(context)) {

                Log.d("Utils", "未授权");
                return null; // 或者抛出异常，提示用户授予权限
            }
            return handleImageOnKitKat(context,data);
        } else {
            // 4.4以下系统使用这个方法处理图片
            //return handleImageBeforeKitKat(context,data);
            return null;
        }
    }


    @TargetApi(19)
    private static String handleImageOnKitKat(Context context, Intent data) {
        String imagePath = null;
        Uri uri = data.getData();
        Log.d("Utils", "URI: " + uri.toString());
        if (DocumentsContract.isDocumentUri(context, uri)) {
            if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                String docId = DocumentsContract.getDocumentId(uri);
                String[] split = docId.split(":");
                String type = split[0];
                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }
                String selection = "_id=?";
                String[] selectionArgs = new String[]{split[1]};
                imagePath = getImagePath(context, contentUri, selection, selectionArgs);
            } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(DocumentsContract.getDocumentId(uri)));
                imagePath = getImagePath(context, contentUri, null, null);
            }
        } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            imagePath = getImagePath(context, uri, null, null);
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            imagePath = uri.getPath();
        }
        Log.d("Utils", "Image Path: " + imagePath);
        return imagePath;
    }


/*    private static String handleImageBeforeKitKat(Context context,Intent data) {
        Uri uri = data.getData();
        String imagePath = getImagePath(context,uri, null);
        return imagePath;
    }*/



    @SuppressLint("Range")
    private static String getImagePath(Context context, Uri uri, String selection, String[] selectionArgs) {
        String path = null;
        Cursor cursor = context.getContentResolver().query(uri, null, selection, selectionArgs, null);
        Log.d("Utils", "Cursor: " + cursor);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    path = cursor.getString(columnIndex);
                    Log.d("Utils", "Path from cursor: " + path);
                }
            } finally {
                cursor.close();
            }
        }
        return path;
    }

    @TargetApi(30)
    private static boolean hasManageExternalStoragePermission(Context context) {
        return Environment.isExternalStorageManager();
    }




}



